<template>
  <basic-container v-loading="loading">
    <div class="el-page-header haruyuki-mb-20">
      <div
        class="el-page-header__left haruyuki-flex-center haruyuki-size-18"
        @click="goBackPage"
      >
        <i class="el-icon-arrow-left"></i>
        <div class="haruyuki-size-16">返回</div>
      </div>
      <div class="el-page-header__content">{{ $route.name }}</div>
    </div>
    <div>
      <register-form :registerForm="registerForm" :regType="dialogType" ref="toRegisterForm"/>
    </div>
    <span class="haruyuki-flex-x-end">
      <el-button
        icon="el-icon-check"
        size="medium"
        type="primary"
        v-preventClick
        @click="handleRegister"
        >提 交</el-button
      >
    </span>
  </basic-container>
</template>

<script>
import {
  addPosIncoming,
  editPosIncoming,
  getDetailPosIncoming,
} from "@/api/rb-service/posinfo/pos_info";
import registerForm from "@/components/rb-merchant/register-form";

export default {
  components: {
    registerForm,
  },
  data() {
    return {
      rpiNo: "",
      dialogType: "", //add or edit
      registerForm: {
        contactsName: "", //姓名
        contactsPhone: "", //联系方式
        posSn: "", //SN号
        aisleCode: "", //通道号
      },
    };
  },
  watch: {
    "$route.query.e": {
      handler(val) {
        if (val) {
          const param = JSON.parse(this.$crypto.decrypt(val));
          const { rpiNo } = param;
          this.dialogType = "edit";
          this.rpiNo = rpiNo;
          this.infoDetails(this.rpiNo);
        } else {
          this.dialogType = "add";
        }
      },
      immediate: true,
    }
  },
  created() {
    // if (this.$route.query.rpiNo) {
    //   this.dialogType = "edit";
    //   this.rpiNo = this.$route.query.rpiNo;
    //   this.infoDetails(this.rpiNo);
    // } else {
    //   this.dialogType = "add";
    // }
  },
  methods: {
    handleRegister() {
      this.$refs["toRegisterForm"].$refs["registerForm"].validate((valid) => {
        if (valid) {
          const _this = this;
          if (_this.dialogType === "add") {
            addPosIncoming(_this.registerForm).then(
              (res) => {
                if (res.data.code == "200") {
                    _this.$message({
                      type: "success",
                      message: "商户登记提交成功!",
                    });
                    _this.goBackPage();
                } else {
                  _this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else if (_this.dialogType === "edit") {
            editPosIncoming(_this.registerForm).then(
              (res) => {
                if (res.data.code == "200") {
                    _this.$message({
                      type: "success",
                      message: "商户登记编辑成功!",
                    });
                    _this.goBackPage();
                } else {
                  _this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    infoDetails(rpiNo) {
      getDetailPosIncoming(rpiNo).then((res) => {
        this.registerForm = res.data.data;
      });
    },
    goBackPage() {
      this.$router.$avueRouter.closeTag();
      this.$router.back();
    }
  },
};
</script>

<style>
</style>